//
//  SSMineTableView.m
//  textDemo
//
//  Created by Esther on 2017/2/18.
//  Copyright © 2017年 Esther. All rights reserved.
//

#import "SSMineTableView.h"
#import "SSMineCell.h"

@interface SSMineTableView()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic, strong) NSArray *contentTableSource;
@property(nonatomic, strong) UIView *loginFooterView;
@property(nonatomic, strong) UIButton *logoutButton;

@end

@implementation SSMineTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    self = [super initWithFrame:frame style:style];
    
    if (self) {
        self.backgroundColor = [UIColor ss_colorWithRed:242 green:242 blue:242];
        self.delegate = self;
        self.dataSource = self;
        self.bounces = NO;
        // 取出tableViewCell的下滑线
        self.separatorStyle = UITableViewCellSeparatorStyleNone;

    }
    
    
    return self;
    
}

//- (void)layoutSubviews{
//    [super layoutSubviews];
//    
//    
//    NSDictionary *loginDict = [[NSUserDefaults standardUserDefaults] valueForKey:@"ISLOGIN"];
//    if (!loginDict.count) {
//        [self.loginFooterView addSubview:self.logoutButton];
//        
//#pragma mark - 约束
//        __weak typeof (self) weakSelf = self;
//        [_logoutButton mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(weakSelf.loginFooterView.mas_left).offset(50);
//            make.right.equalTo(weakSelf.loginFooterView.mas_right).offset(-50);
//            make.height.mas_equalTo(45);
//            make.top.equalTo(weakSelf.loginFooterView.mas_top).offset(42);
//        }];
//        self.tableFooterView = self.loginFooterView;
//        self.sectionFooterHeight = 150.0;
//    }else{
//        self.tableFooterView = nil;
//        self.sectionFooterHeight = 150.0;
//        
//    }
//    
//}

#pragma mark -
#pragma mark - 监听
- (void)logoutMethod{
    if (self.logoutBlock) {
        self.logoutBlock();
    }
}
#pragma mark -
#pragma mark - delegate & dataSoure
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSDictionary *loginDict = [[NSUserDefaults standardUserDefaults] valueForKey:@"ISLOGIN"];
    if (!loginDict.count) {
        return 6;
    }else{
        return 4;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SSMineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[SSMineCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        
    }
    cell.cellDict = self.contentTableSource[indexPath.row];
    
    if (indexPath.row == 3) {
        
        UIImageView *nextImage = [cell valueForKey:@"nextImageView"];
        nextImage.hidden = YES;
        
        UILabel *phoneLabel = [[UILabel alloc] init];
        phoneLabel.textColor = [UIColor ss_colorWithRed:123 green:123 blue:123];
        phoneLabel.text = @"440-100-1111";
        phoneLabel.font = [UIFont systemFontOfSize:15.0f];
        [cell addSubview:phoneLabel];
        
        #pragma mark - 约束
        __weak typeof (cell) weakSelf = cell;
        [phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.mas_right).offset(-15);
            make.centerY.equalTo(weakSelf.mas_centerY);
            make.size.mas_equalTo(CGSizeMake(100.0, 15.0));
        }];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%zd",indexPath.row);
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    NSDictionary *loginDict = [[NSUserDefaults standardUserDefaults] valueForKey:@"ISLOGIN"];
    if (!loginDict.count) {
        [self.loginFooterView addSubview:self.logoutButton];
        
        #pragma mark - 约束
        __weak typeof (self) weakSelf = self;
        [_logoutButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.loginFooterView.mas_left).offset(50);
            make.right.equalTo(weakSelf.loginFooterView.mas_right).offset(-50);
            make.height.mas_equalTo(45);
            make.top.equalTo(weakSelf.loginFooterView.mas_top).offset(42);
        }];
        return self.loginFooterView;
    }else{
        return nil;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 100.0;
}



#pragma mark -
#pragma mark - 懒加载

- (NSArray *)contentTableSource{
    if (!_contentTableSource) {
        _contentTableSource = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"SSMineTable.plist" ofType:nil]];
        
    }
    return _contentTableSource;
    
}

- (UIView *)loginFooterView{
    if (!_loginFooterView) {
        _loginFooterView = [[UIView alloc] init];
        _loginFooterView.backgroundColor = [UIColor ss_colorWithRed:242 green:242 blue:242];
        
    }
    return _loginFooterView;
}

- (UIButton *)logoutButton{
    if (!_logoutButton) {
        _logoutButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_logoutButton setImage:[UIImage imageNamed:@"我的界面退出登录按钮"] forState:(UIControlStateNormal)];
        
        [_logoutButton addTarget:self action:@selector(logoutMethod) forControlEvents:(UIControlEventTouchUpInside)];
        
    }
    return _logoutButton;
}
@end
